<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center;">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 6px; overflow: hidden">
      <div style="flex: 1;">
        <img src="@/assets/logo.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center;">
        <el-form :model="user" :rules="rules" ref="loginRef" @submit.native.prevent style="width: 75%">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">
            欢迎登录管理系统
          </div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" v-model="user.username"
                      ref="begin"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex;">
              <el-input prefix-icon="el-icon-circle-check" style="flex: 1" v-model="user.code"></el-input>
              <div style="flex: 1; height: 40px;">
                <valid-code @update:value="getCode"></valid-code>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="success" native-type="submit" @click="login" style="width: 100%">登 录
            </el-button>
          </el-form-item>
          <div style="display: flex; justify-content: space-between;">
            <div>还没有注册？请 <span style="color: blue; cursor: pointer;"
                                     @click="$router.push('/register')">注册</span></div>
            <div style="cursor: pointer; color: blue;" @click="resetDialog=true">忘记密码</div>
          </div>
        </el-form>
      </div>
    </div>

    <!-- 忘记密码弹窗-->
    <el-dialog title="重置密码" :visible.sync="resetDialog" width="30%"
               :before-close="handleClose">
      <el-form :model="resetForm" :rules="resetRules" ref="resetRef" label-width="20%" @submit.native.prevent>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="resetForm.username"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="resetForm.phone"></el-input>
        </el-form-item>
        <el-button style="visibility: hidden" native-type="submit" @click="resetPassword"></el-button>
      </el-form>
      <span slot="footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="resetPassword">确 定</el-button>
            </span>
    </el-dialog>

  </div>
</template>

<script>
import ValidCode from "@/components/ValidCode.vue";

export default {
  name: 'LoginView',
  components: {
    ValidCode
  },
  data() {
    // 验证码校验规则
    var validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code.toLowerCase()) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }

    return {
      code: '',//验证码正确答案
      user: {
        username: '',
        password: '',
        code: '',//用户输入的验证码
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          // {min:6,max:18,message:'密码长度为6到18位',trigger:'blur'}
        ],
        code: [
          {validator: validateCode, trigger: 'blur'}
        ]
      },
      resetDialog: false,
      resetForm: {
        username: '',
        phone: '',
      },
      resetRules: {
        username: [{required: true, message: '请填写用户名', trigger: 'blur'}],
        phone: [{required: true, message: '请填写手机号', trigger: 'blur'}]
      },
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs['begin'].focus()
    })
  },
  created() {
  },
  methods: {
    getCode(code) {
      this.code = code;
      // console.log(code)
    },
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          this.$request.post('/login', this.user).then(res => {
            if (res.code === '200') {
              localStorage.setItem('user', JSON.stringify(res.data))
              this.$message.success('登录成功')
              this.$router.push('/home')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    resetPassword() {
      this.$refs.resetRef.validate(valid => {
        if (valid) {
          this.$request.put('/reset', this.resetForm).then(res => {
            if (res.code === '200')
              this.$message.success('重置成功')
            else this.$message.error(res.msg)
          })
          this.handleClose()
        }
      })
    },
    handleClose() {
      this.resetDialog = false
      this.$refs.resetRef.resetFields()
    }
  }
}
</script>

<style scoped>

</style>
